//通用事件,涵数

var emptyFunc = function(){return true;},
	formatURI = function(str){
		//把id=1&sid=2转换为{id:1, sid:2}
		var result = {};
		if( str.indexOf('&') != -1 ){
			str.split('&').forEach(function(item, index){
				var paramArr = item.split('=');
				result[paramArr[0]] = paramArr[1];
			});
		}else{
			var paramArr = str.split('=');
			result[paramArr[0]] = paramArr[1];
		}
		return result;
	};

/**
 * @param {Object} ourl  删除的API地址
 * @param {Object} send   要传输入的数据
 * @param {Object} parseTarget   要删除的名称,类,id ,标签名 div.abc
 */
function removeElement(ourl, send, parseTarget){
	var sendData = formatURI( send );
	if( !ourl ) return ;
	var that = this;
	parent.layer.confirm('您确定要删除改行数据?', {}, function(){
		$.ajax({
			url:ourl,
			type:'post',
			data:sendData,
			dataType:'json'
		}).done(function(evt){
			if( that !== window ){
				$(that).parents(parseTarget).remove();
			}else{
				throw new Error("请绑定在target上!");  
			}
		}).fail(function(){
			parent.layer.alert("链接服务器失败");
		});
	});
}

function openIframe(ourl,title, opt){
	opt = opt || {};
	if( !ourl  ) return ;
	title = title || '影伴';
	var opts = $.extend(true, {
		type: 2,
	    title: title,
	    shadeClose: true,
	    shade: 0.8,
	    skin: 'layui-layer-molv',  //默绿色
	    area: ['75%', '90%'],
	    content: ourl 
	}, opt);
	parent.layer.open(opts);
}
//$.alert = parent.layer.alert || layer.alert;
$.msg = function(content, type){
	if( type == 'success' ){
		parent.layer.msg(content,{icon:1});
	}
	if( type == 'error' ){
		parent.layer.msg(content,{icon:2});
	}
	if( type == 'warning' ){
		parent.layer.msg(content,{icon:3});
	}
}


/*
 * layer 弹窗  
 * data-layer="iframe" url="template.html" title="等待开工"   
 */
$('[data-dialog]').click(function(){
	var type = $(this).attr('data-dialog');
	if( type === 'iframe' ){
		var ourl = $(this).attr('url'),
			title = $(this).attr('title') || '';
		openIframe(ourl, title);
	}
});



//时间
/*
 * <input placeholder="开始日期" class="form-control input-sm layer-date" id="start">
		至
	<input placeholder="结束日期" class="form-control input-sm layer-date" id="end">
 */
   var start={
        elem:"#start",
        format:"YYYY-MM-DD",
        max:"2099-06-16",
        istime:false,
        istoday:false,
        choose:function(datas){
            end.min=datas;
            end.start=datas
        }
    };
    var end={
        elem:"#end",
        format:"YYYY-MM-DD",
        max:"2099-06-16 23",
        istime:false,
        istoday:false,
        choose:function(datas){
            start.max=datas
        }
    };
//  laydate(start);
//  laydate(end);
    



/*
 * 全选
 * <input type="checkbox" name="" class="allCheck" />
 * <input type="checkbox" name="" class="childCheck" />
 */
$('input.allCheck').click(function(){
	if( this.checked ){
		$('input.childCheck').each(function(){this.checked = true;});
	}else{
		$('input.childCheck').each(function(){this.checked = false;});
	}
});



/*
 * 全局设定ajax
 * 
 */
$(function(){
	var index ; 
	$(document).ajaxStart(function(){
		index = parent.layer.load(0, {
			    shade: [0.2,'#000'] //0.1透明度的白色背景
			});
	});
	$(document).ajaxComplete(function(){
		parent.layer.close(index);
	});
})


/*
 * 在子页面打开一个新的tab页面
 * <a href="createOrder.html" data-name="a1" data-title="新增订单" class="createTab" >新增订单</a>
 */

$('.createTab').click(function(evt){
	evt.preventDefault();
	var href = $(this).attr('href'),
		title = $(this).attr('data-title') || $(this).text() || '影伴',
		name = $(this).attr('data-name') || new Date().getTime();
	var flag = true;
	var pl = parent.document.querySelectorAll('.J_menuTab');  //父 tab
	var pi = parent.document.querySelectorAll('.J_mainContent .J_iframe');  //父 iframe
	
	$(pl).each(function(){
		if($(this).data("id") == href) {
			if(!$(this).hasClass("active")) {
				$(this).addClass("active").siblings(".J_menuTab").removeClass("active");
				$(pi).each(function(){
					if( $(this).data('id') == 'href' ){
						$(this).show().siblings(".J_iframe").hide();
						return false
					}
				});
			}
			flag = false;
			return false;
		}
	});
	
	if( flag ){
		var p = '<a href="javascript:;" class="active J_menuTab" data-id="' + href + '">' + title + ' <i class="fa fa-times-circle"></i></a>';
		$(pl).removeClass('active');
		var n = '<iframe class="J_iframe" name="iframe' + name + '" width="100%" height="100%" src="' + href + '" frameborder="0" data-id="' + href + '" seamless></iframe>';
		$(parent.document.querySelector('.J_mainContent')).find("iframe.J_iframe").hide().parents(".J_mainContent").append(n);
		$(parent.document.querySelector('.J_menuTabs .page-tabs-content')).append(p);
	}else{
//		$('.J_iframe [name="iframe' + name + '"]').
		$(pi).hide();
		$(pi).each(function(){
			if( this.name == "iframe" + name && $(this).attr('data-id') == href ){
				$(this).css('display', 'inline');
			}
		})
	}
	return false;
	
})

